<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/formSelects-v4.css" media="all">
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="component-form-element">
                <div class="layui-tab-content" style="height: auto">
                    <div class="layui-tab-item layui-show" id="onecontent">
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">账号</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="username" lay-verify="username"
                                               autocomplete="off" placeholder="请输入账号"
                                               class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">用户名</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="name"
                                               autocomplete="off" placeholder="请输入用户名"
                                               class="layui-input" lay-verify="name">
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">联系方式</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="phone"
                                               autocomplete="off" placeholder="请输入联系方式"
                                               class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">所属部门</label>
                                    <div class="layui-input-block">
                                        <select name="depTid" xm-select="depTid" xm-select-search="" lay-verify="departmenttid">
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                        </div>
<%--                        <div class="layui-form-item">--%>
<%--                            <div class="layui-col-lg6">--%>
<%--                                <div class="layui-col-xs11">--%>
<%--                                    <label class="layui-form-label">职务</label>--%>
<%--                                    <div class="layui-input-block">--%>
<%--                                        <select name="positionTid" xm-select="positionTid" xm-select-search=""--%>
<%--                                                xm-select-radio="">--%>
<%--                                        </select>--%>
<%--                                    </div>--%>
<%--                                </div>--%>
<%--                                &lt;%&ndash;                                <div class="layui-col-xs1">&ndash;%&gt;--%>
<%--                                &lt;%&ndash;                                    <div class="layui-form-mid layui-word-aux"&ndash;%&gt;--%>
<%--                                &lt;%&ndash;                                         style="color: red !important;padding: 10px !important;">*&ndash;%&gt;--%>
<%--                                &lt;%&ndash;                                    </div>&ndash;%&gt;--%>
<%--                                &lt;%&ndash;                                </div>&ndash;%&gt;--%>
<%--                            </div>--%>
<%--                        </div>--%>

                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">性别</label>
                                    <div class="layui-input-block">
                                        <input type="radio" name="sex" value="男" title="男" checked="">
                                        <input type="radio" name="sex" value="女" title="女">
                                        <input type="radio" name="sex" value="保密" title="保密">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs6">
                                    <label class="layui-form-label">初始密码</label>
                                    <div class="layui-input-block">
                                        <input type="password" name="password" lay-verify="password"
                                               placeholder="请输入密码" autocomplete="off" class="layui-input"
                                               value="123456">
                                    </div>
                                </div>
                                <div class="layui-col-xs6">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 9px !important;">*
                                    </div>
                                    <div class="layui-form-mid layui-word-aux">默认初始密码为123456</div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">状态</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" checked="" name="state" lay-skin="switch"
                                               lay-filter="switchTest" lay-text="正常|禁止">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">备注</label>
                                    <div class="layui-input-block">
                                            <textarea class="layui-textarea" name="remarks"
                                                      placeholder="请输入备注"></textarea>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-footer" style="left: 0;padding: 0px;">
                        <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">立即提交</button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'laydate', 'formSelects'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate
            , layer = layui.layer
            , formSelects = layui.formSelects;

        $.ajax({
            type: "post",
            url: "<%=request.getContextPath()%>/rest/system/getDepartmentList",
            dataType: "json",
            async: false,
            success: function (msg) {
                // var select = document.getElementById("depTid");
                // $.each(msg.data, function (i, n) {
                //     select.options.add(new Option(n.departmentname, n.tid));
                // })
                // form.render('select');
                formSelects.data('depTid', 'local', {
                    arr: msg
                });
            }
        })

        var flag = true;
        formSelects.on('depTid', function (id, vals, val, isAdd, isDisabled) {
            // alert("当前选择了: " + JSON.stringify(vals));
            var arr = JSON.stringify(vals)
            var jsonObj = JSON.parse(arr);//转换为json对象

            if (flag) {
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/system/getPosition",
                    dataType: "json",
                    async: false,
                    success: function (obj) {
                        formSelects.data('positionTid', 'local', {
                            arr: obj
                        });
                        flag = false;
                    }
                })
            }

            if (jsonObj.length == 0) {
                flag = true
                formSelects.value('positionTid', []);
                formSelects.data('positionTid', 'local', {
                    arr: []
                });
            }
        }, true);


        //自定义验证规则
        form.verify({
            username: function (value, item) {
                if (value == "") {
                    return '请输入账号';
                }
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '账号不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '账号首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '账号不能全为数字';
                }
            },
            name: function (value, item) {
                if (value == "") {
                    return '请输入用户名';
                }
            },
            departmenttid: function (value, item) {
                if (value == "") {
                    return '请选择所属部门';
                }
            },
            password: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
        });


        //第一种方法添加用户
        form.on('submit(*)', function (data) {
            var field = data.field//当前容器的全部表单字段，名值对形式：{name: value}
            var username = field.username
            var flag = true;
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/getUserByUserNameExsit",
                dataType: "json",
                async: false,
                data: "username=" + username,
                success: function (msg) {
                    flag = msg;
                }
            })
            if (flag) {
                var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
                $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/system/addUser",
                    data: field,
                    dataType: "json",
                    async: false,
                    success: function (obj) {
                        setTimeout(function () {
                            layer.close(index);
                            if (obj = true) {
                                layer.msg('添加成功');
                            } else {
                                layer.msg(obj.errorMsg);
                            }
                        }, 2000);
                        setTimeout(function () {
                            var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                            parent.layer.close(indexs);//关闭弹出层
//                        window.parent.location.reload();//刷新父页面
                        }, 4000);
                    }
                })
            } else {
                layer.msg("账号已存在", {icon: 5, anim: 6});
                return false;
            }
            return false;
        });


    });
</script>
</body>
</html>